package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.List;

import liumengbaio.lolzhangyoubao.Activity.InFo.RecentBattle;
import liumengbaio.lolzhangyoubao.R;

/**
 * Created by lanouhn on 16/9/19.
 */
public class AnimRFRecyclerViewAdapter extends RecyclerView.Adapter<AnimRFRecyclerViewAdapter.ViewHolder> {
    private Context mContext;
    private List<RecentBattle.DataBean.DataBean2.BattleListBean> mlist;

    public AnimRFRecyclerViewAdapter(Context mContext, List<RecentBattle.DataBean.DataBean2.BattleListBean> mlist) {
        this.mContext = mContext;
        this.mlist=mlist;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_recent_battle_recycle_view, null);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mlist.get(position).getGame_type() == 3) {
            holder.tv_leixing.setText("匹配");
        } else if (mlist.get(position).getGame_type() == 4) {
            holder.tv_leixing.setText("排位");
        } else {
            holder.tv_leixing.setText("极地大乱斗");
        }
        if (mlist.get(position).getWin() == 1) {
            holder.tv_win_flag.setBackgroundResource(R.mipmap.bg_match_win_flag);

        } else if (mlist.get(position).getWin() == 2) {
            holder.tv_win_flag.setBackgroundResource(R.mipmap.bg_match_fail_flag);
        }
        holder.tv_recent_time.setText(mlist.get(position).getBattle_time().substring(5,16));
        if(mlist.get(position).getFlag()==1){
            holder.tv_mvp.setBackgroundResource(R.mipmap.bg_match_mvp_flag);
        }else {
            holder.tv_mvp.setBackgroundColor(Color.parseColor("#00000000"));
        }
        String result = readFile("hero/heroTgpIds.json");
        try {
            JSONObject object = new JSONObject(result);
            String id = object.getString(mlist.get(position).getChampion_id() + "");
            String hero_result_one = readFile("hero/" + id + ".json");
            JSONObject obj_one = new JSONObject(hero_result_one);
            if (obj_one.has("pic_url") && (obj_one.isNull("pic_url") == false)) {
                Picasso.with(mContext).load(obj_one.getString("pic_url")).into(holder.iv_recent_battle);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public int getItemCount() {
        Log.e("001","所有比赛"+mlist.size());
        return mlist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private ImageView iv_recent_battle;
        private TextView tv_leixing, tv_win_flag, tv_mvp, tv_recent_time;

        public ViewHolder(View itemView) {
            super(itemView);
            iv_recent_battle = (ImageView) itemView.findViewById(R.id.iv_recent_battle);
            tv_leixing = (TextView) itemView.findViewById(R.id.tv_leixing);
            tv_win_flag = (TextView) itemView.findViewById(R.id.tv_win_flag);
            tv_mvp = (TextView) itemView.findViewById(R.id.tv_mvp);
            tv_recent_time = (TextView) itemView.findViewById(R.id.tv_recent_time);
        }
    }
    private String readFile(String fileName) {
        String result = "";
        try {
            // 读取assets中的文件，文件内容写入一个输入流里边
            InputStream is = mContext.getAssets().open(fileName);
            // 按行读取
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);
            // 创建一个临时的字符串来保存每一行的数据
            String line = "";
            // 循环读取文件内容，每次读取一行，如果读取到文件末尾，则读取的内容为null
            while ((line = br.readLine()) != null) {
                result += line;// 将每次读取的内容给最终的result
            }
            // 关闭流,先开的关
            br.close();
            isr.close();
            is.close();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
}
